<template>
  <div class="app-container">
    <!-- 操作 -->
    <el-row :gutter="20">
      <el-col :span="9">
        <el-input v-model="query.name" placeholder="请输入博客名称查找" @keyup.enter.native="searchData()" />
      </el-col>
      <el-col :span="6">
        <el-button type="primary" @click="searchData()">查询</el-button>
        <el-tooltip class="item" effect="dark" content="添加数据" placement="top-start">
          <el-button type="primary" @click="showDialog = true">添加</el-button>
        </el-tooltip>
      </el-col>
    </el-row>

    <!-- 表格数据 -->
    <el-table :data="tableData" style="width: 100%; margin: 20px 0" stripe border :default-sort="{ prop: 'createTime', order: 'descending' }">
      <el-table-column align="center" label="序号" width="60">
        <template slot-scope="scope">
          {{ scope.$index + addIndex }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="博客名称" prop="blogname" />
      <el-table-column align="center" label="博客地址" prop="blogaddress" />
      <el-table-column align="center" label="图片地址" prop="pictureaddress" />
      <el-table-column align="center" label="创建时间" prop="createTime" sortable />
      <el-table-column align="center" label="更新时间" prop="updateTime" sortable />
      <el-table-column align="center" label="操作" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="edit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination @current-change="handleCurrentChange" @size-change="handleSizeChange" :total="total" :current-page.sync="query.current" :page-size="query.pageSize" :page-sizes="[10, 20, 30, 40]" background layout="total, sizes, prev, pager, next, jumper" style="text-align: center" />

    <!-- 弹框 -->
    <el-dialog :title="getTitle" :visible.sync="showDialog" :close-on-click-modal="false" @close="closeDialog('infoForm')" @close-on-press-escape="closeDialog('infoForm')">
      <el-form ref="infoForm" :model="infoForm" label-width="120px" :rules="rules">
        <el-form-item label="博客名称" prop="blogname">
          <el-input v-model="infoForm.blogname" placeholder="请输入博客名称" />
        </el-form-item>
        <el-form-item label="博客地址" prop="blogaddress">
          <el-input v-model="infoForm.blogaddress" placeholder="请输入博客地址" />
        </el-form-item>
        <el-form-item label="图片地址" prop="pictureaddress">
          <el-input v-model="infoForm.pictureaddress" placeholder="请输入图片地址" />
        </el-form-item>
      </el-form>
      <div style="text-align: right">
        <el-button type="danger" @click="closeDialog('infoForm')">取消</el-button>
        <el-button v-if="getId" type="primary" @click="update('infoForm')">更新</el-button>
        <el-button v-else type="primary" @click="save('infoForm')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import friendApi from '@/api/friend'

export default {
  data() {
    return {
      query: {
        current: 1,
        pageSize: 10,
        name: ''
      },
      total: 0,

      tableData: [],
      addIndex: 1,

      showDialog: false,
      fileList: [],
      infoForm: {
        id: '',
        blogaddress: '',
        blogname: '',
        pictureaddress: ''
      },
      // 表单验证
      rules: {
        blogaddress: [{ required: true, message: '博客地址不能为空', trigger: 'blur' }],
        blogname: [{ required: true, message: '博客名称不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    // 弹框标题
    getTitle: function () {
      if (this.infoForm.id !== '') {
        return '编辑'
      } else {
        return '添加'
      }
    },
    // 展示的按钮
    getId: function () {
      if (this.infoForm.id !== '') {
        return true
      } else {
        return false
      }
    }
  },
  created() {
    this.list()
  },
  methods: {
    // 查询
    searchData() {
      this.query.current = 1
      this.addIndex = this.query.pageSize * (this.query.current - 1) + 1
      this.list()
    },

    // 页码查询
    handleCurrentChange(val) {
      this.query.current = val
      this.addIndex = this.query.pageSize * (this.query.current - 1) + 1
      this.list()
    },

    // 显示条数查询
    handleSizeChange(val) {
      this.query.pageSize = val
      this.query.current = 1
      this.addIndex = this.query.pageSize * (this.query.current - 1) + 1
      this.list()
    },

    // 获取列表
    list() {
      friendApi.list(this.query).then(response => {
        if (response.code === 20000) {
          let data = response.data.data
          this.tableData = data.records

          this.query.current = data.current
          this.query.pageSize = data.size
          this.total = data.total
        } else {
          this.$message.error(response.message)
        }
      })
    },

    // 删除
    handleDel(id) {
      this.$confirm('此操作将永久删除被选中的记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const form = new FormData()
        form.append('id', id)

        friendApi.delById(form).then(response => {
          if (response.code === 20000) {
            this.$message.success(response.message)
            //刷新页面
            this.list()
          } else {
            this.$message.error(response.message)
          }
        })
      })
    },

    // 添加
    save(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          const form = new FormData()
          form.append('id', this.infoForm.id)
          form.append('blogaddress', this.infoForm.blogaddress)
          form.append('blogname', this.infoForm.blogname)
          form.append('pictureaddress', this.infoForm.pictureaddress)

          friendApi.save(form).then(response => {
            if (response.code === 20000) {
              this.$message.success(response.message)
              // 关闭弹框
              this.showDialog = false
              //刷新页面
              this.list()
            } else {
              this.$message.error(response.message)
            }
          })
        }
      })
    },

    // 编辑按钮
    edit(row) {
      this.showDialog = true
      this.infoForm.id = row.id
      this.infoForm.blogaddress = row.blogaddress
      this.infoForm.blogname = row.blogname
      this.infoForm.pictureaddress = row.pictureaddress
    },

    // 更新
    update(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          const form = new FormData()
          form.append('id', this.infoForm.id)
          form.append('blogaddress', this.infoForm.blogaddress)
          form.append('blogname', this.infoForm.blogname)
          form.append('pictureaddress', this.infoForm.pictureaddress)

          friendApi.update(form).then(response => {
            if (response.code === 20000) {
              this.$message.success(response.message)
              // 关闭弹框
              this.showDialog = false
              //刷新页面
              this.list()
            } else {
              this.$message.error(response.message)
            }
          })
        }
      })
    },

    // 关闭弹框
    closeDialog(formName) {
      this.$refs[formName].resetFields()
      this.showDialog = false
      this.infoForm.id = ''
      this.infoForm.blogaddress = ''
      this.infoForm.blogname = ''
      this.infoForm.pictureaddress = ''
    }
  }
}
</script>
